package com.asurplus.system.service.impl;

import com.asurplus.common.layui.LayuiTableResult;
import com.asurplus.common.shiro.ShiroUtils;
import com.asurplus.common.utils.PasswordUtils;
import com.asurplus.common.utils.R;
import com.asurplus.system.entity.SysParamConf;
import com.asurplus.system.entity.SysUserInfo;
import com.asurplus.system.mapper.SysUserInfoMapper;
import com.asurplus.system.service.SysParamConfService;
import com.asurplus.system.service.SysUserInfoService;
import com.asurplus.system.vo.SysUserInfoVO;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 用户信息表 服务实现类
 * </p>
 *
 * @author lizhou
 * @since 2020-07-06
 */
@Service
public class SysUserInfoServiceImpl extends ServiceImpl<SysUserInfoMapper, SysUserInfo> implements SysUserInfoService {

    @Autowired
    private SysParamConfService sysParamConfService;

    @Override
    public LayuiTableResult list(Integer page, Integer limit, SysUserInfo sysUserInfo) {
        QueryWrapper<SysUserInfoVO> queryWrapper = new QueryWrapper<>();
        if (StringUtils.isNotBlank(sysUserInfo.getName())) {
            queryWrapper.like("a.name", sysUserInfo.getName());
        }
        if (null != sysUserInfo.getDeptId() && 0 <= sysUserInfo.getDeptId()) {
            queryWrapper.eq("a.dept_id", sysUserInfo.getDeptId());
        }
        if (StringUtils.isNotBlank(sysUserInfo.getAccount())) {
            queryWrapper.like("a.account", sysUserInfo.getAccount());
        }
        if (StringUtils.isNotBlank(sysUserInfo.getPhone())) {
            queryWrapper.like("a.phone", sysUserInfo.getPhone());
        }
        if (null != sysUserInfo.getSex()) {
            queryWrapper.eq("a.sex", sysUserInfo.getSex());
        }
        if (null != sysUserInfo.getStatus()) {
            queryWrapper.eq("a.status", sysUserInfo.getStatus());
        }
        queryWrapper.orderByDesc("a.create_time");
        return new LayuiTableResult<>(this.baseMapper.listSysUserInfoVO(new Page<>(page, limit), queryWrapper));
    }

    @Override
    public R add(SysUserInfo sysUserInfo) {
        LambdaQueryWrapper<SysUserInfo> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SysUserInfo::getAccount, sysUserInfo.getAccount());
        List<SysUserInfo> userInfoList = this.list(queryWrapper);
        if (null != userInfoList && !userInfoList.isEmpty()) {
            return R.no("该账户已经存在，请检查后提交");
        }
        SysParamConf sysParamConf = sysParamConfService.saveGetSysParamConf();
        String defaultPassword = "123456";
        if (null != sysParamConf && StringUtils.isNotBlank(sysParamConf.getDefaultPassword())) {
            defaultPassword = sysParamConf.getDefaultPassword();
        }
        String password = PasswordUtils.getPassword(defaultPassword, sysUserInfo.getAccount());
        sysUserInfo.setPassword(password);
        sysUserInfo.setCreateUser(ShiroUtils.getSysUserId());
        this.save(sysUserInfo);
        return R.ok();
    }

    @Override
    public R update(SysUserInfo sysUserInfo) {
        sysUserInfo.setUpdateUser(ShiroUtils.getSysUserId());
        this.updateById(sysUserInfo);
        return R.ok();
    }

    @Override
    public R info(SysUserInfo sysUserInfo) {
        this.updateById(sysUserInfo);
        return R.ok();
    }

    @Override
    public R delete(Long id) {
        this.removeById(id);
        return R.ok();
    }

    @Override
    public R updateStatus(Long id, Integer status) {
        SysUserInfo userInfo = this.getById(id);
        userInfo.setStatus(status);
        this.updateById(userInfo);
        return R.ok();
    }

    @Override
    public R updatePassword(String oldPassword, String password) {
        if (oldPassword.equals(password)) {
            return R.no("新密码不能与旧密码相同");
        }
        SysParamConf sysParamConf = sysParamConfService.saveGetSysParamConf();
        if (null != sysParamConf && StringUtils.isNotBlank(sysParamConf.getDefaultPassword())) {
            if (password.equals(sysParamConf.getDefaultPassword())) {
                return R.no("为了您的账户安全，请尽量不要使用系统初始密码");
            }
        }
        // 获取登录用户
        SysUserInfo sysUserInfo = this.getById(ShiroUtils.getSysUserId());
        oldPassword = PasswordUtils.getPassword(oldPassword, sysUserInfo.getAccount());
        if (!oldPassword.equals(sysUserInfo.getPassword())) {
            return R.no("旧密码不正确");
        }
        password = PasswordUtils.getPassword(password, sysUserInfo.getAccount());
        sysUserInfo.setPassword(password);
        this.updateById(sysUserInfo);
        return R.ok();
    }

    @Override
    public R updatePassword(Integer id, String password) {
        if (null == id || 0 == id) {
            return R.no("请选择需要操作的用户");
        }
        if (StringUtils.isBlank(password)) {
            return R.no("请输入密码");
        }
        SysUserInfo sysUserInfo = this.getById(id);
        if (null == sysUserInfo) {
            return R.no("用户信息错误");
        }
        sysUserInfo.setPassword(PasswordUtils.getPassword(password, sysUserInfo.getAccount()));
        this.updateById(sysUserInfo);
        return R.ok();
    }
}
